<?php

	class EventItemCommentDataAccessMySQL implements iEventItemCommentDataAccess {
		
		public function buildObject($result) {
			
			$eventItemComment = new EventItemComment();
			
			$eventItemComment->setEventItemCommentId($result->eventItemCommentId);
			$eventItemComment->setEventItemId($result->eventItemId);
			$eventItemComment->setComment($result->comment);
			$eventItemComment->setDate(formatDate($result->date));
			
			$eventItemUser = new EventItemUser();
			$eventUser = new EventUser();
			$user = new User();
			
			$user->setUserId($result->userId);
			$user->setEmail($result->email);
			$user->setName($result->name);
			
			$eventUser->setUser($user);
			$eventUser->setUserRegistrationCode($result->registrationCode);
			
			$eventItemUser->setEventUser($eventUser);
			
			$eventItemComment->setCommentUser($eventItemUser);
			
			return $eventItemComment;
	
		}
		
		public function selectEventItemCommentByEventItemId($eventItemId, $dataBaseConnection) {
			$resultArray = array();
			
			$parameters["EVENTITEMID"] = $eventItemId;
			
			$query = buildQuery("querySelectComment", $parameters);
			if (!is_bool($query)) {
				if ($result = $dataBaseConnection->query($query)) {
					while($row = mysql_fetch_object($result)) {
						array_push($resultArray, $this->buildObject($row));	
					}
					return $resultArray;	
				}
				else {
					return NULL;
				}
			} else {
				return NULL;
			}
		}
		
		public function saveComment($comment, $dataBaseConnection) {
			
			$parameters = array();
			
			$parameters["EVENTITEMID"] = $comment->getEventItemId();
			$parameters["REGISTRATIONCODE"] = $comment->getCommentUser()->getEventUser()->getUserRegistrationCode();
			$parameters["COMMENT"] = "'" . $comment->getComment() . "'";
			$query = buildQuery("queryInsertComment", $parameters);
			
			if (!is_bool($query)) {
				if ($result = $dataBaseConnection->query($query)) {
						
					return $dataBaseConnection->getLastInsertId();
				}
				else {
					return -1;
				}
			} else {
				return -1;
			}
		}
		
		public function deleteEventItemComment($eventItemComment, $dataBaseConnection) {
			
			if ($eventItemComment->getEventItemCommentId() != null){
				$parameters["EVENTITEMCOMMENTID"] = $eventItemComment->getEventItemCommentId();
			}else {
				$parameters["EVENTITEMCOMMENTID"] = "NULL";
			}
			
			if ($eventItemComment->getEventItemId() != null){
				$parameters["EVENTITEMID"] = $eventItemComment->getEventItemId();
			}else {
				$parameters["EVENTITEMID"] = "NULL";
			}
			
			if ($eventItemComment->getCommentUser()->getEventUser()->getUserRegistrationCode() != null){
				$parameters["REGISTRATIONCODE"] = $eventItemComment->getCommentUser()->getEventUser()->getUserRegistrationCode();
			}else {
				$parameters["REGISTRATIONCODE"] = "NULL";
			}

			$query = buildQuery("queryDeleteRequestItemComment", $parameters);
			if (!is_bool($query)) {
				$result = $dataBaseConnection->query($query);
				
				return $result;
			} else {
				return false;
			}
		}
		
	}

?>